package com.hongxun.budget.ui.adapter;

import android.content.Context;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckedTextView;
import android.widget.Spinner;
import android.widget.TextView;

import com.hongxun.budget.R;
import com.hongxun.entity.LineName;
import com.hongxun.greenentity.tk.BDataDictionaryEntity;
import com.hongxun.greenentity.tk.BMaterialEntity;
import com.hongxun.interfaces.ResponseListener;

import java.util.List;

/**
 * SpinnerAdapter
 */

public class SpinnerAdapter<T> extends ArrayAdapter<T> implements AdapterView.OnItemSelectedListener {

    List<T> objects;
    Spinner spinner;
    T item;
    int resource;
    LayoutInflater infalter;
    ResponseListener listener;

    public SpinnerAdapter(@NonNull Context context, @LayoutRes int resource, @NonNull List<T> objects) {
        super(context, resource, objects);
        this.objects = objects;
        this.resource = resource;
        if (objects.size() > 0)//设置默认项
            item = objects.get(0);
        infalter = LayoutInflater.from(context);
    }

    public SpinnerAdapter(@NonNull Context context, @NonNull List<T> objects, Spinner spinner) {
        this(context, R.layout.spinner_checked_text, objects);
        this.spinner = spinner;
        spinner.setOnItemSelectedListener(this);
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        if (convertView == null) {
            convertView = infalter.inflate(resource, null);
            CheckedTextView label = convertView.findViewById(android.R.id.text1);
            T obj = objects.get(position);
            if (obj instanceof String) {
                label.setText((String) objects.get(position));
            } else if (obj instanceof BDataDictionaryEntity) {
                label.setText(((BDataDictionaryEntity) obj).getDValue());
            } else if (obj instanceof LineName) {
                label.setText(((LineName) obj).getName());
            } else if (obj instanceof BMaterialEntity) {
                label.setText(((BMaterialEntity) obj).getMName());
            }
        }
        return convertView;
    }

    @Override
    public View getDropDownView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.spinner_item_layout, null);
        TextView label = (TextView) view
                .findViewById(R.id.spinner_item_label);
        T obj = objects.get(position);
        if (obj instanceof String) {
            label.setText((String) objects.get(position));
        } else if (obj instanceof BDataDictionaryEntity) {
            label.setText(((BDataDictionaryEntity) obj).getDValue());
        } else if (obj instanceof LineName) {
            label.setText(((LineName) obj).getName());
        }else if (obj instanceof BMaterialEntity) {
            label.setText(((BMaterialEntity) obj).getMName());
        }
        if (spinner.getSelectedItemPosition() == position) {
            view.setBackgroundResource(android.R.color.holo_orange_dark);
        } else {
            view.setBackgroundResource(R.color.white);
        }
        return view;
    }

    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
        item = objects.get(i);
        if (listener != null) listener.response(i, objects.get(i));
    }

    public SpinnerAdapter<T> setListener(ResponseListener listener) {
        this.listener = listener;
        return this;
    }

    @Override
    public void onNothingSelected(AdapterView<?> adapterView) {
    }

    public T getSelectedItem() {
        return item;
    }
}
